#include <stdio.h>
#include <stdlib.h>

#define MAX 5
typedef int DataType;

typedef struct {
    DataType list[MAX];
    int top;
} Stack;


Stack *init() {

    Stack *stack = (Stack *) malloc(sizeof(Stack));
    stack->top = -1;

    return stack;
}

int isEmpty(Stack *stack) {
    return stack->top == -1;
}

int isFull(Stack *stack) {
    return stack->top == MAX - 1;
}

void push(Stack *stack, DataType value) {

    if (isFull(stack)) {
        printf("stack is full! top={%d}\n", stack->top);
        return;
    }

    stack->list[++stack->top] = value;
}

DataType pop(Stack *stack) {
    if (isEmpty(stack)) {
        printf("stack is empty!\n");
        return 0;
    }
    return stack->list[stack->top--];
}

void toString(Stack *stack) {

    printf("[");
    for (int i = 0; i <= stack->top; i++) {
        printf("%d,", stack->list[i]);
    }
    printf("]\n");
}


int main() {
    printf("Hello, World!\n");

    Stack *stack = init();

    push(stack, 1);
    push(stack, 2);
    push(stack, 3);
    push(stack, 4);
    push(stack, 5);
    push(stack, 12);

    toString(stack);

    for (int i = 0; i <= 5; i++) {
        printf("pop num=%d,value=%d\n", i, pop(stack));
    }

    toString(stack);

    return 0;
}
